//
//  AddressViewController.swift
//  Box
//
//  Created by mn on 2025/3/31.
//
import UIKit

class AddressViewController: BaseViewController {
    var selectAddress:((AddressData) -> Void)? = nil
    private let tableView = UITableView()
    var addressDatas = [AddressData]()
    lazy var button: InsetBorderButton = {
        let button = InsetBorderButton(type: .custom)
        button.setTitle("新增地址", for: .normal)
        button.setTitleColor(.fromHex("#CAFE67"), for: .normal)
        button.layer.cornerRadius = 7.5
        button.titleLabel?.font = .systemFont(ofSize: 16)
        button.backgroundColor = .black
        button.borderColor = .white
        button.addAction { [weak self] in
            let vc = EditAddressViewController()
            self?.navigationController?.push(vc)
        }
        return button
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.headerBeginRefreshing()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "地址管理"
        tableView.backgroundColor = .fromHex("#F4F6F8")

        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(nav.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        tableView.rowHeight = 148
        tableView.dataSource = self
        tableView.delegate = self
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.registerCell(AddressCell.self)
        tableView.addMJHeader {
            [weak self] in
            self?.getAddressList()
        }
        tableView.headerBeginRefreshing()
        view.addSubview(button)
        button.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(46.5)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-24)
            make.height.equalTo(52)
        }
    }
    func getAddressList() {
        APIService.request(api: .getMyAddressList) {
            [weak self] (result,response) in
            self?.tableView.endHeaderRefreshing()
            if result.success ,let list: AddressResponse = Decoder.data(response){
                self?.addressDatas = list.data
                self?.tableView.reloadData()
            } else {
                self?.toast(result.message)
            }
        }
    }
    
}
extension AddressViewController : UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        addressDatas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: AddressCell = tableView.cell(indexPath)
        cell.data = addressDatas[indexPath.row]
        cell.deleteButton.addAction {
            [weak self] in
            self?.deleteAddress(indexPath.row)
        }
        cell.defaultButton.addAction {
            [weak self] in
            self?.updateDefaultAddress(indexPath.row)
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectAddress?(addressDatas[indexPath.row])
        navigationController?.pop()
    }
    func deleteAddress(_ index: Int) {
        APIService.request(api: .deleteAddress(id: addressDatas[index].id)) {
            [weak self] (result,_) in
            if result.success {
                self?.addressDatas.remove(at: index)
                self?.tableView.reloadData()
            }
        }
    }
    func updateDefaultAddress(_ index: Int) {
        APIService.request(api: .updateDefaultAddress(id: addressDatas[index].id)) {
            [weak self] (result,_) in
            if result.success {
                self?.getAddressList()
            }
        }
    }
    
}
